package com.toidep.config;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.Log;

public class ConfigUtility {

	public static int scrWidth, scrHeight;
	public static float scale = 0.0f;
	public static int densityDpi = 0;
	public static int os_version;
	public static String sdcardPath = ""; // thuong la /mnt/sdcard

	public static final int PAGE_SIZE = 5;

	public static int size_full_width = 130;
	public static int size_half_width = 130;
	
	public static int size_icon = 32;

	public static float scale_thumbs_news_width = 1/4f;
	
	public static float scale_size_horizontal_thumbs_item = 2/3f;
//	public static float scale_size_vertical_thumbs_item = 3/2f;

	public static float scale_size_list_horizontal = 2/3f;
	public static float scale_size_list_vertical = 3/2f;
	
	public static float scale_size_top_image = 0.3f;
	
	/**
	 * Lay thong tin cua thiet bi
	 */
	public static void getConfig(Activity activity) {
		DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

		os_version = android.os.Build.VERSION.SDK_INT;
		sdcardPath = "" + android.os.Environment.getExternalStorageDirectory();
		
		scrHeight = metrics.heightPixels;
		scrWidth = metrics.widthPixels;
		densityDpi = metrics.densityDpi;
		scale = metrics.scaledDensity;
		
	    double x = Math.pow(metrics.widthPixels/metrics.xdpi,2);
	    double y = Math.pow(metrics.heightPixels/metrics.ydpi,2);
	    double screenInches = Math.sqrt(x+y);
		
		switch (densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			break;
		case DisplayMetrics.DENSITY_MEDIUM:
			break;
		case DisplayMetrics.DENSITY_HIGH:
			break;
		case DisplayMetrics.DENSITY_XHIGH:
			break;
		case DisplayMetrics.DENSITY_XXHIGH:
			break;
		case DisplayMetrics.DENSITY_XXXHIGH:
			break;
		}
		
		Log.e("Device inf", "Width " + scrWidth + ", Height " + scrHeight);
	    Log.e("screen inches","" + screenInches + " inch ~> x, y = " + x + " cm, " + y + " cm");
		Log.e("scale", "" + scale);
		Log.e("densityDpi", "" + densityDpi);
		Log.e("Android Version", "" + os_version);
		Log.e("sdcardPath", sdcardPath);
		
		if (screenInches > 5.5) {
			size_full_width = 1280;
			size_half_width = 720;
			size_icon = 114;
			
		} else if (screenInches >= 4) {
			size_full_width = 720;
			size_half_width = 480;
			size_icon = 72;
			
		} else {
			size_full_width = 480;
			size_half_width = 240;
			size_icon = 48;
		}
	}

	/**
	 * Tao time stamp de danh ID cho cac Fragment
	 */
	public static String createTimeStamp() {

		return "" + System.currentTimeMillis();

	}

	/**
	 * Kiem tra xem file co ton tai (trong sdcard) hay khong?
	 */
	public static boolean fileExistsInSD(String sFileName) {

		java.io.File file;
		try {
			file = new java.io.File(sFileName);
		} catch (Exception e) {
			return false;
		}
		return file.exists();

	}

	/**
	 * Xoa File (trong sdcard)
	 */
	public static void deleteFileInSDcard(String sFileName) {

		if (fileExistsInSD(sFileName)) { // Neu ton tai file thi moi xoa
			java.io.File file;
			try {
				file = new java.io.File(sFileName);
				if (file != null) {
					file.delete();
				}
			} catch (Exception e) {
			}
		}

	}
	
	public static void deleteFileInternal(String sFileName) {
		try {
			java.io.File file;
			file = new java.io.File(sFileName);
			if (file != null) {
				file.delete();
			}
		} catch (Exception e) {
		}
	}

	/**
	 * Lay % de update len SeekBar
	 */
	public static int getProgressPercentage(long currentDuration, long totalDuration) {
		Double percentage = (double) 0;

		long currentSeconds = (int) (currentDuration / 1000);
		long totalSeconds = (int) (totalDuration / 1000);

		// calculating percentage
		percentage = (((double) currentSeconds) / totalSeconds) * 100;

		// return percentage
		return percentage.intValue();
	}

	/**
	 * Convert tu milisecond thanh gio dang HH:mm:ss
	 */
	public static String milliSecondsToTimer(long milliseconds) {
		String finalTimerString = "";
		String secondsString = "";

		// Convert total duration into time
		int hours = (int) (milliseconds / (1000 * 60 * 60));
		int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
		int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
		// Add hours if there
		if (hours > 0) {
			finalTimerString = hours + ":";
		}

		// Prepending 0 to seconds if it is one digit
		if (seconds < 10) {
			secondsString = "0" + seconds;
		} else {
			secondsString = "" + seconds;
		}

		finalTimerString = finalTimerString + minutes + ":" + secondsString;

		// return timer string
		return finalTimerString;
	}

	/**
	 * Convert tu milisecond thanh gio dang HH:mm:ss
	 */
	public static String convertSecondToHour(int totalSecs) {
		int hours = (int) totalSecs / 3600;
		int minutes = (int) (totalSecs % 3600) / 60;
		int seconds = (int) totalSecs % 60;

		String timeString = hours + ":" + minutes + ":" + seconds;
		return timeString;
	}

}
